﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Web.Editors.ASPx;
using DevExpress.ExpressApp.Web.Editors;
using DevExpress.ExpressApp.Model;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTreeList;
using DevExpress.ExpressApp;
using System.Web.UI;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;

namespace ECRM.Module.Web
{
    [PropertyEditor(typeof(XPBaseObject),false)]
    public class ASPxTreePropertyEditor : ASPxObjectPropertyEditorBase
    {
        private WebLookupEditorHelper helper;
        public ASPxTreePropertyEditor(Type objectType, IModelMemberViewItem model)
            : base(objectType, model)
        {
            skipEditModeDataBind = true;
        }
        //1、集成了借口之后你需要实现的只有这一个重写方法，目的就在于要返回一个自定义的属性编辑器(也就是一个控件)
        protected override WebControl CreateEditModeControlCore()
        {
            return CreateTreeDropDown();
        }
        TreeDropDown CreateTreeDropDown()
        {
            try
            {
                //2、具体的控件需要用到一个下拉书的控件，我在另一个类中实现了这个控件。
                TreeDropDown treeDropDown = new TreeDropDown();

                treeDropDown.TreeList.DataSource = helper.CreateListView(CurrentObject).CollectionSource.List;
                treeDropDown.TreeList.KeyFieldName = "Oid";
                treeDropDown.TreeList.ParentFieldName = "Parent!Key";
                treeDropDown.TreeList.ClientInstanceName = "TreeList";
                treeDropDown.TreeList.Width = Unit.Parse("100%");
                treeDropDown.ClientInstanceName = "DropDownEdit";
                treeDropDown.TreeList.PreviewFieldName = "Name";
                TreeListTextColumn col = new TreeListTextColumn();
                col.Name = "Name";
                col.FieldName = "Name";
                treeDropDown.TreeList.Columns.Add(col);
                treeDropDown.ReadOnly = true;
                treeDropDown.TreeList.SettingsBehavior.AutoExpandAllNodes = true;
                treeDropDown.TreeList.CustomCallback += new TreeListCustomCallbackEventHandler(TreeList_CustomCallback);
                return treeDropDown;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        //7、由于ASPxTreeList没有节点点击时间，所以我用了客户端的点击事件，让他返回一个回调方法。
        void TreeList_CustomCallback(object sender, TreeListCustomCallbackEventArgs e)
        {
            //这里是用户在用户改变选择项的时候，重新为属性赋值。以保存对象。
            ((TreeDropDown)Editor).Value = helper.GetObjectByKey(CurrentObject, e.Argument);
            EditValueChangedHandler(sender, EventArgs.Empty);
        }

        // 6 、再绑定数据时需要用到一个helper对象，用来创建数据列表
        public override void Setup(IObjectSpace objectSpace, XafApplication application)
        {
            base.Setup(objectSpace, application);
            if (MemberInfo.IsPersistent)
            {
                helper = new WebLookupEditorHelper(application, objectSpace, MemberInfo.MemberTypeInfo, Model);
            }
            else
            {
                helper = new WebLookupNonPersistentEditorHelper(application, objectSpace, MemberInfo.MemberTypeInfo, Model);
            }
        }
        //3、之后我生成了这个类。
        class TreeDropDown : DevExpress.Web.ASPxEditors.ASPxDropDownEdit
        {
            ASPxTreeList treeList;
            public ASPxTreeList TreeList
            {
                get
                {
                    return treeList;
                }
            }

            public TreeDropDown()
            {

                treeList = new ASPxTreeList();
                //4、在这个类中我需要向他添加一个包含有ASPxTreeList控件的模板，所以要实现一个ITemplate接口
                this.DropDownWindowTemplate = new TreeListTemplate(treeList);
            }
            protected override void OnPreRender(EventArgs e)
            {
                string script = @" <script type='text/javascript'>
           function RowClickHandler(s, e) {
               DropDownEdit.SetKeyValue(e.nodeKey);
               TreeList.GetNodeValues(e.nodeKey, 'Name', setDropDownText);
               DropDownEdit.HideDropDown();
               TreeList.PerformCallback(e.nodeKey);
           }
           function setDropDownText(value) {
               DropDownEdit.SetText(value)
           }</script>  ";
                this.RegisterScriptBlock("", script);
                treeList.ClientSideEvents.NodeClick = "RowClickHandler";
                base.OnPreRender(e);
            }

            //5、实现ITemplate接口完成一个模板项
            class TreeListTemplate : ITemplate
            {
                ASPxTreeList treeList;
                public TreeListTemplate(ASPxTreeList treeList)
                {
                    this.treeList = treeList;
                }
                public void InstantiateIn(Control container)
                {
                    container.Controls.Add(treeList);
                }
            }
        }
    }

    
}
